之前有稍微提到 IndexStore DB
首先是 libIndexStore.dylib
位置
通常是位於:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libIndexStore.dylib
我們可透過取得某工具的路徑 xcrun --find sourcekit-lsp
,再以相對路徑取得 libIndexStore.dylib
xcrun --find sourcekit-lsp
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp
derived path
.build
應該可以隨意指定
因此先放在 NSTemporaryDirectory
db 初始化完畢後
需要做 indexing
, 也就是 db.pollForUnitChangesAndWait()
//
// IndexDB.swift
//
import Foundation
import IndexStoreDB
public struct IndexStore {
private static let defaultLibPath: String = "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libIndexStore.dylib"
public let db: IndexStoreDB
public init?(path: String) {
guard let lspPath: String = Exec.run(
"/usr/bin/xcrun",
"--find", "sourcekit-lsp"
).string else { return nil }
let indexStoreLibPath: URL = URL(fileURLWithPath: lspPath)
.deletingLastPathComponent()
.deletingLastPathComponent()
.appendingPathComponent("lib/libIndexStore.dylib")
let libPath: String = indexStoreLibPath.path
do {
try self.db = IndexStoreDB(
storePath: path,
databasePath: NSTemporaryDirectory() + "index_\(getpid())",
library: IndexStoreLibrary(dylibPath: libPath)
)
db.pollForUnitChangesAndWait()
} catch {
print(error)
return nil
}
}
}